package WebSpider;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

public class pa1 {
    public static void main(String[] args)throws IOException {
        ArrayList<String> arr=new ArrayList<>();
        for (int k=0;k<5;k++){
            String url="https://sh.esf.fang.com/house/i3"+k+1+"/?rfss=1-891cc3b63f2eacb230-a5";
            Document document= Jsoup.parse(new URL(url),30000);
            Elements tit_shop= document.getElementsByClass("tit_shop");
            Elements tel_shop= document.getElementsByClass("tel_shop");
            Elements add_shop= document.getElementsByClass("add_shop");
            Elements bg_none_icon_dt= document.getElementsByClass("bg_none icon_dt");
            Elements price_right= document.getElementsByClass("price_right");
            for (int i=0;i<tit_shop.size();i++){
                String str1=tit_shop.get(i).text();
                String str2=tel_shop.get(i).text();
                String str3=add_shop.get(i).text();String str4=bg_none_icon_dt.get(0).text();
                String str5=price_right.get(i).text();
                String str=str1+","+str2+","+str3+","+str4+","+str5;
                arr.add(str);
            }
        }
        HSSFWorkbook wb=new HSSFWorkbook();
        HSSFSheet sheet=wb.createSheet("二手房信息");
        for (int j=0;j<arr.size();j++){
            sheet.createRow(j).createCell(0).setCellValue(arr.get(j));
        }
        try{
            FileOutputStream fout=new FileOutputStream("E:/Web/Web项目/WebProject/src/main/java/WebSpider/二手房信息.csv");
            wb.write(fout);
            fout.close();
        }catch (Exception e){
            e.printStackTrace();
           }
    }
}



